package cn.dkdoo.utils.templateDefault.redis;

import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author nicky
 * @date 2021/5/14 下午12:02
 */
@Deprecated
public class RollbackListOperations<K, V> implements ListOperations<K, V> {

    private RedisTemplate<K, V> template;
    private RollbackRedisTemplate<K, V> templateRollback;

    public RollbackListOperations(RedisTemplate<K, V> template, String beanName) {
        this.template = template;
        this.templateRollback = new RollbackRedisTemplate<K, V>((StringRedisTemplate) template, beanName);
    }

    public RollbackListOperations(RedisTemplate<K, V> template) {
        this.templateRollback = new RollbackRedisTemplate<K, V>((StringRedisTemplate) template);
        this.template = template;
    }


    @Override
    public List<V> range(K key, long start, long end) {
        List<V> res = this.template.opsForList().range(key, start, end);
        this.templateRollback.range(key, start, end);
        return res;
    }

    @Override
    public void trim(K key, long start, long end) {
        this.template.opsForList().trim(key, start, end);
        this.templateRollback.trim(key, start, end);
    }

    @Override
    public Long size(K key) {
        Long res = this.template.opsForList().size(key);
        this.templateRollback.size(key);
        return res;
    }

    @Override
    public Long leftPush(K key, V value) {
        Long res = this.template.opsForList().leftPush(key, value);
        this.templateRollback.leftPush(key, value);
        return res;
    }

    @Override
    public Long leftPushAll(K key, V... values) {
        Long res = this.template.opsForList().leftPushAll(key, values);
        this.templateRollback.leftPushAll(key, values);
        return res;
    }

    @Override
    public Long leftPushAll(K key, Collection<V> values) {
        Long res = this.template.opsForList().leftPushAll(key, values);
        this.templateRollback.leftPushAll(key, values);
        return res;
    }

    @Override
    public Long leftPushIfPresent(K key, V value) {
        Long res = this.template.opsForList().leftPushIfPresent(key, value);
        this.templateRollback.leftPushIfPresent(key, value);
        return res;
    }

    @Override
    public Long leftPush(K key, V pivot, V value) {
        Long res = this.template.opsForList().leftPush(key, pivot, value);
        this.templateRollback.leftPush(key, pivot, value);
        return res;
    }

    @Override
    public Long rightPush(K key, V value) {
        Long res = this.template.opsForList().rightPush(key, value);
        this.templateRollback.rightPush(key, value);
        return res;
    }

    @Override
    public Long rightPushAll(K key, V... values) {
        Long res = this.template.opsForList().rightPushAll(key, values);
        this.templateRollback.rightPushAll(key, values);
        return res;
    }

    @Override
    public Long rightPushAll(K key, Collection<V> values) {
        Long res = this.template.opsForList().rightPushAll(key, values);
        this.templateRollback.rightPushAll(key, values);
        return res;
    }

    @Override
    public Long rightPushIfPresent(K key, V value) {
        Long res = this.template.opsForList().rightPushIfPresent(key, value);
        this.templateRollback.rightPushIfPresent(key, value);
        return res;
    }

    @Override
    public Long rightPush(K key, V pivot, V value) {
        Long res = this.template.opsForList().rightPush(key, pivot, value);
        this.templateRollback.rightPush(key, pivot, value);
        return res;
    }

    @Override
    public void set(K key, long index, V value) {
        this.template.opsForList().set(key, index, value);
        this.templateRollback.set(key, index, value);
    }

    @Override
    public Long remove(K key, long i, Object value) {
        Long res = this.template.opsForList().remove(key, i, value);
        this.templateRollback.remove(key, i, value);
        return res;
    }

    @Override
    public V index(K key, long index) {
        V res = this.template.opsForList().index(key, index);
        this.templateRollback.index(key, index);
        return res;
    }

    @Override
    public V leftPop(K key) {
        V res = this.template.opsForList().leftPop(key);
        this.templateRollback.leftPop(key);
        return res;
    }

    @Override
    public V leftPop(K key, long timeout, TimeUnit unit) {
        V res = this.template.opsForList().leftPop(key, timeout, unit);
        this.templateRollback.leftPop(key, timeout, unit);
        return res;
    }

    @Override
    public V rightPop(K key) {
        V res = this.template.opsForList().rightPop(key);
        this.templateRollback.rightPop(key);
        return res;
    }

    @Override
    public V rightPop(K key, long timeout, TimeUnit unit) {
        V res = this.template.opsForList().rightPop(key, timeout, unit);
        this.templateRollback.rightPop(key, timeout, unit);
        return res;
    }

    @Override
    public V rightPopAndLeftPush(K sourceKey, K destinationKey) {
        V res = this.template.opsForList().rightPopAndLeftPush(sourceKey, destinationKey);
        this.templateRollback.rightPopAndLeftPush(sourceKey, destinationKey);
        return res;
    }

    @Override
    public V rightPopAndLeftPush(K sourceKey, K destinationKey, long timeout, TimeUnit unit) {
        V res = this.template.opsForList().rightPopAndLeftPush(sourceKey, destinationKey, timeout, unit);
        this.templateRollback.rightPopAndLeftPush(sourceKey, destinationKey, timeout, unit);
        return res;
    }

    @Override
    public RedisOperations<K, V> getOperations() {
        return this.template.opsForList().getOperations();
    }
}
